﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using TTSBLL;
using System.Data;
using TTS;
using TTSModel;

public partial class T0002052_Add : System.Web.UI.Page
{
    private Assemble a;
    private UnitType u;
    private string message;
    private IList<UnitTypeInfo> assembleVersionListT0002052;
    private IList<UnitTypeInfo> assembleVersionListT0002051;
    private string T0002051_SN { get { return ViewState["T0002051_SN"].ToString(); } set { ViewState["T0002051_SN"] = value; } }
    private string T0002052_SN { get { return ViewState["T0002052_SN"].ToString(); } set { ViewState["T0002052_SN"] = value; } }
    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }

    public T0002052_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            assembleVersionListT0002052 = u.LoadEntities("T0002052");
            assembleVersionListT0002051 = u.LoadEntities("T0002051");
            this.T0002052_Label.Text = assembleVersionListT0002052[0].TcodeVersion + "," + assembleVersionListT0002052[1].TcodeVersion;
            this.T0002051_Label.Text = assembleVersionListT0002051[0].TcodeVersion + "," + assembleVersionListT0002051[1].TcodeVersion;
           // this.T0002051_Label.Text = u.UnitType_GetbyTcode("T0002051").TcodeVersion;
            //this.T0002052_Label.Text = u.UnitType_GetbyTcode("T0002052").TcodeVersion;
        }
        this.TextBox_T0002051.Focus();
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("DM.aspx");
    }
    protected void TextBox_T0002051_TextChanged(object sender, EventArgs e)
    {
        T0002051_SN = this.TextBox_T0002051.Text.Trim();
        if (SNHelper.SNCheck(T0002051_SN, "T0002051", out message))
        {
            if (SNHelper.SNCheckVersionTwo(T0002051_SN, this.T0002051_Label.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002051_SN, "T0002051", out message))
                {
                    this.TextBox_T0002051.Enabled = false;
                    this.TextBox_T0002052.Enabled = true;
                    this.TextBox_T0002052.Focus();
                    return;
                }
            }
           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002051.Text = "";
        this.TextBox_T0002051.Focus();
    }
    protected void TextBox_T0002052_TextChanged(object sender, EventArgs e)
    {
        T0002052_SN = this.TextBox_T0002052.Text.Trim();
        if (SNHelper.SNCheck(T0002052_SN, "T0002052", out message))
        {
            if (SNHelper.SNCheckVersionTwo(T0002052_SN, this.T0002052_Label.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002052_SN, "T0002052", out message))
                {
                    this.TextBox_T0002052.Enabled = false;
                    this.ConfirmSubmit.Enabled = true;
                    return;
                }
            }
            
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002052.Text = "";
        this.TextBox_T0002052.Focus();
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
         if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002051_SN, "T0002051", out message))
         {
              if (ProConditionHelper.CheckMainSNsPrecondition(T0002052_SN, "T0002052", out message))
              {
                  Unit u = new Unit();
                  u.Insert(new UnitInfo() { SN = T0002052_SN, Tcode = "T0002052", IsLeaf = true, IsRoot = true });
                  u.Insert(new UnitInfo() { SN = T0002051_SN, Tcode = "T0002051", IsLeaf = true, IsRoot = true });
                  AssembleInfo ainfo = new AssembleInfo()
                  {
                      ID = 0,
                      SN = T0002052_SN,
                      IsRoot = true,
                      RecordTime = DateTime.Now,
                      Tcode = "T0002052",
                      Userid = "111112"
                  };

                  dt.Rows.Clear();
                  dr = dt.NewRow();
                  dr["SubSN"] = T0002051_SN;
                  dr["Remark"] = "DM_backboard_PCBA";
                  dt.Rows.Add(dr);

                  if (a.Assemble_Insert(ainfo, dt))
                  {
                      message = "录入成功";
                      this.TextBox_T0002051.Text = "";
                      this.TextBox_T0002052.Text = "";
                      this.TextBox_T0002051.Enabled = true;
                      this.TextBox_T0002051.Focus();                     
                  }
                  else
                  {
                      message = "录入失败，请联系相关人员";
                  }
              }
         }
        
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
}